package com.ptswitch.material.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.stereotype.Service;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.service.ErrorInputCacheService;

@Service
public class ErrorInputCacheServiceImpl implements ErrorInputCacheService {
    private Map<Integer, List<ContractItem>> receiveItems = new HashMap<Integer, List<ContractItem>>();
    private Map<Integer, List<ContractItem>> examineItems = new HashMap<Integer, List<ContractItem>>();
    private Map<Integer, List<ContractItem>> returnItems = new HashMap<Integer, List<ContractItem>>();

    @Override
    public List<ContractItem> getExamineItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        return examineItems.get(hash);
    }

    @Override
    public List<ContractItem> getReceiveItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        return receiveItems.get(hash);
    }

    @Override
    public List<ContractItem> getReturnItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        return returnItems.get(hash);
    }

    @Override
    public void cacheExamineItems(long id, Uzer user, List<ContractItem> data) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        examineItems.put(hash, data);
    }

    @Override
    public void cacheReceiveItems(long id, Uzer user, List<ContractItem> data) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        receiveItems.put(hash, data);
    }

    @Override
    public void cacheReturnItems(long id, Uzer user, List<ContractItem> data) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        returnItems.put(hash, data);
    }

    @Override
    public void removeExamineItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        examineItems.remove(hash);
    }

    @Override
    public void removeReceiveItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        receiveItems.remove(hash);
    }

    @Override
    public void removeReturnItems(long id, Uzer user) {
        HashCodeBuilder b = new HashCodeBuilder();
        int hash = b.append(id).append(user.getUsername()).toHashCode();
        returnItems.remove(hash);
    }

}
